;; $Id: oaxaca.scm 474 2014-04-30 21:59:24Z alan.watson.f $

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(unless (and (finder-initialized?) (finder-idle?))
  (log-info "waiting for finder.")
  (finder-wait))
(log-info "finder ready.")

(unless (housekeeping-initialized?)
  (log-info "waiting for housekeeping.")
  (housekeeping-wait))
(log-info "housekeeping ready.")

(unless (and (instrument-initialized?) (instrument-idle?))
  (log-info "waiting for instrument.")
  (instrument-wait))
(log-info "instrument ready.")

(unless (and (lamp-initialized?) (lamp-idle?))
  (log-info "waiting for lamp.")
  (lamp-wait))
(log-info "lamp ready.")

(unless (and (wfc-initialized?) (wfc-idle?))
  (log-info "waiting for wfc.")
  (wfc-wait))
(log-info "wfc ready.")

(unless (and (wfs-initialized?) (wfs-idle?))
  (log-info "waiting for wfs.")
  (wfs-wait))
(log-info "wfs ready.")

(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring finder.")
(log-info "")

;; Determined 2013-10-29.
(finder-set-scale! 0.239)

(finder-set-window-center! 660 565)
(finder-configure! 'full-frame-1)

;; Determined 2014-04-08 UTC.
(finder-set-r-vector! #(660.0 565.0))

(finder-set-search-radius!     (exact (round (/ 30.0 (finder-scale)))))
(finder-set-refinement-radius! (exact (round (/  5.0 (finder-scale)))))

(log-info "finished configuring finder.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring instrument.")
(log-info "")

;; Determined 2013-10-29.
(instrument-set-scale! 0.0302)

(instrument-set-window-center! 608 484)
(instrument-configure! 'full-frame-1)

(instrument-set-search-radius!     (exact (round (/ 10.0 (instrument-scale)))))
(instrument-set-refinement-radius! (exact (round (/  2.0 (instrument-scale)))))

;; Determined 2014-03-12 UTC.
(instrument-set-r-vector! #(607.41851725316 484.910488554834))

;; Determined 2014-03-11 UTC.
#;(instrument-set-i-matrix!
   #(
     #(-1.66666666666667 -127.777777777778)
     #(-118.333333333333 7.22222222222222)
     #(0 0)
     #(0 0)
     #(0 0)
     #(0 0)
     #(0 0)))

;; Determined 2014-03-11 UTC.
#;(instrument-set-c-matrix!
   #(
     #( -4.773e-04 -7.820e-03 )
     #( -8.444e-03 +1.101e-04 )
     #(0 0)
     #(0 0)
     #(0 0)
     #(0 0)
     #(0 0)))

#;(instrument-set-g-vector!
   #(0.25 0.25 0 0 0 0 0))

; Determined 2014-04-08 UTC
(instrument-set-r-vector! #(555.670018280429 579.37804787282))
(instrument-set-window-center! 556 580)

; Determined 2014-04-08 UTC
(instrument-set-i-matrix!
 #(
   #(0.555555555555555 -130)
   #(-118.888888888889 1.11111111111111)
   #(0 0)
   #(0 0)
   #(0 0)))

; Determined 2014-04-08 UTC
(instrument-set-c-matrix!
 #(
   #( -7.189e-05 -7.693e-03 )
   #( -8.412e-03 -3.595e-05 )
   #(0 0)
   #(0 0)
   #(0 0)))

(instrument-set-g-vector!
 #(0.25 0.25 0 0 0))

(log-info "finished configuring instrument.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring wfs.")
(log-info "")

(wfs-set-window-center! 732 404)
(wfs-configure! 'full-frame-1)

(wfs-set-search-radius! 8)
(wfs-set-refinement-radius! 4)

(define (vector-shift dx dy old-v)
  (let* ((n (vector-length old-v))
         (new-v (make-vector n 0.0)))
    (do ((i 0 (+ i 2)))
        ((= i n) new-v)
      (vector-set! new-v (+ i 0) (+ (vector-ref old-v (+ i 0)) dx))
      (vector-set! new-v (+ i 1) (+ (vector-ref old-v (+ i 1)) dy)))))


;; For 5x5 square pupil.

;; Determined 2014-03-12 UTC.
#;(wfs-set-r-vector!
   (vector-shift -2 +2
                 #(
                   556.495072894169 448.440807235421
                   556.91720145967 465.109684996435
                   557.284968960507 481.882600272971

                   572.887863365824 431.297075821616
                   574.39006809058 498.128773260585

                   589.560009034722 430.937701484569
                   591.143916855091 497.689039628704

                   606.368086224003 430.429952724655
                   607.911685796704 497.267040526093

                   623.47704553523 446.787776303743
                   623.92938616375 463.38005258863
                   624.237794907732 480.128474655454
                   )))

;; For single-spot mode.
;(wfs-set-window-center! 620 480)

;; Determined 2014-03-12 UTC.
#;(wfs-set-i-matrix!
   #(
     #(-6.65524318918127 0.677382380348356 -6.40025009519211 0.720343841419557 -6.83133665512 0.2215116573181 -6.6387642045774 0.39266482390311 -6.94655216223586 0.364646162041811 -6.30338682742559 0.449201066348422 -6.71645722788811 0.552924293709793 -7.04278717773795 -0.0792589194840292 -6.55705009321577 0.681314575209373 -6.81462547322769 0.364098112467293 -6.56761379006815 0.235221455383308 -5.98126512450487 0.447611663213144)
     #(0.662708394741435 6.15973629231931 0.438664775129064 6.32131975001566 0.662520974415855 6.65124988842268 0.766139071983649 6.18590377985668 0.646139584377272 6.72209189015332 0.660395261353971 6.21795173539709 0.494030215750448 6.38955498118159 1.08275062013599 6.84977563541399 0.362204888742055 6.52220671846665 0.67875801351044 6.57889335095973 0.797347048862104 6.69319950762008 0.538036099437381 6.48560164661738)
     #(-1.81072715802343 -1.01659297578985 -1.59889413582241 -0.101535218740811 -1.70013275439266 0.787998381952409 -0.958940066102601 -1.98483357989182 -0.85260432180865 1.81097672427426 -0.00586700097940312 -1.96749657490559 0.0341364161529572 1.75353747995132 0.82497344129249 -2.05280484310323 0.832613006681381 1.60035232654146 1.82869426415125 -1.17302517266176 1.72353220091666 -0.17643220356085 1.6109425897514 0.653190336809351)
     #(-0.465761263743756 -0.355023842866006 -0.220882799148446 -0.466624595672845 -0.0454948261021176 -0.602240193287358 -0.640782796009363 0.00434501467294426 0.284910729516257 -0.434179757393365 -0.536785167911522 0.221076856662751 0.325236220503314 -0.220879240714969 -0.407698145671228 0.432493108553379 0.415290720964292 -0.0160509324566899 -0.0752385451529768 0.539324739019366 0.12555031199096 0.424699455060432 0.443316458491725 0.316526059378385)
     #(-0.317007861554403 0.518841426794425 -0.390630157090518 0.312754016682043 -0.521249574376787 0.0262204905388093 -0.0227755457847402 0.634691664872715 -0.40474914968806 -0.314656397814959 0.174356832170225 0.541196156151214 -0.192505319539214 -0.356585616046543 0.458192324007006 0.441716430177759 0.00920305479513988 -0.440248147349086 0.478387668478398 0.0754188442066805 0.352781437535362 -0.129057488259612 0.40372671296324 -0.368567420026014)
     #(0.0599113959970272 0.404262720991042 -0.178997985736105 0.244224530691035 -0.326772155494057 -0.0622467510534285 0.480564186090528 0.0878037517937318 -0.0530364981676515 -0.3440361978779 0.25448296213286 -0.212275253036965 0.132112667570345 -0.175202965898064 0.0422099687106699 -0.441795680534815 0.220388735799557 0.0427373654158932 -0.287150222077478 -0.192189786301537 -0.205188514039902 0.0796798818006018 -0.207808654659642 0.278304171266126)
     #(0.388927877537567 -0.0580113737660663 0.207563208827539 0.207681132308915 -0.0107953157279302 0.378461067043337 0.167915458454932 -0.425737732002221 -0.281102886537185 0.0556134297591231 -0.161025582515294 -0.307355721712693 -0.124644151269114 -0.128105897816013 -0.427456332087704 -0.0672223087581752 0.0731559258992418 -0.244471285144914 -0.1562521632199 0.324628337200872 0.0963459463223475 0.23460832101744 0.380853519945477 0.0439719651262986)
     ))

;; Determined 2014-03-12 UTC.
#;(wfs-set-c-matrix!
   #(
     #( -1.089e-02 +1.792e-03 -1.073e-02 +2.701e-03 -1.184e-02 +2.978e-03 -1.163e-02 +7.758e-04 -1.412e-02 +3.128e-03 -1.112e-02 +1.016e-03 -1.434e-02 +2.265e-03 -1.272e-02 +8.890e-05 -1.458e-02 +1.275e-03 -1.254e-02 +1.171e-04 -1.273e-02 -6.008e-04 -1.246e-02 -5.470e-04 )
     #( +1.029e-04 +1.134e-02 -2.402e-04 +1.155e-02 +6.985e-04 +1.184e-02 +1.475e-03 +1.037e-02 +2.901e-03 +1.341e-02 +7.220e-04 +9.699e-03 +2.840e-03 +1.433e-02 +6.052e-04 +1.030e-02 +2.413e-03 +1.640e-02 -1.075e-03 +1.226e-02 -6.207e-05 +1.488e-02 -6.937e-04 +1.678e-02 )
     #( -4.477e-02 -1.492e-02 -4.121e-02 +1.570e-03 -4.408e-02 +1.559e-02 -1.968e-02 -3.656e-02 -1.831e-02 +3.553e-02 +1.744e-03 -3.892e-02 +4.859e-03 +3.757e-02 +2.146e-02 -4.278e-02 +2.512e-02 +3.780e-02 +4.191e-02 -2.239e-02 +4.056e-02 +1.675e-03 +3.951e-02 +2.100e-02 )
     #( -1.245e-01 -5.310e-02 -8.436e-02 -1.040e-01 -5.140e-02 -1.849e-01 -1.137e-01 +2.737e-03 +1.019e-01 -1.702e-01 -1.165e-01 +2.782e-02 +1.501e-01 -7.727e-02 -1.049e-01 +6.640e-02 +1.993e-01 +2.240e-02 -4.040e-02 +1.528e-01 +4.121e-02 +1.663e-01 +1.448e-01 +1.663e-01 )
     #( -6.977e-02 +1.299e-01 -1.099e-01 +9.947e-02 -1.770e-01 +3.398e-02 +1.182e-03 +1.240e-01 -1.704e-01 -1.044e-01 +3.751e-02 +1.094e-01 -8.156e-02 -1.414e-01 +1.061e-01 +1.024e-01 +8.896e-03 -1.875e-01 +1.520e-01 +2.739e-02 +1.351e-01 -4.737e-02 +1.811e-01 -1.433e-01 )
     #( -2.249e-02 +2.686e-01 -1.707e-01 +1.392e-01 -2.571e-01 -1.071e-01 +2.870e-01 +6.316e-02 +1.939e-02 -2.869e-01 +1.309e-01 -1.374e-01 +1.782e-01 -1.219e-01 -7.876e-03 -2.797e-01 +2.680e-01 +8.239e-02 -2.054e-01 -5.126e-02 -1.074e-01 +1.602e-01 -6.262e-02 +3.109e-01 )
     #( +2.471e-01 +2.858e-03 +1.069e-01 +1.733e-01 -6.806e-02 +2.632e-01 +1.121e-01 -2.516e-01 -2.529e-01 -1.489e-02 -1.115e-01 -1.720e-01 -1.141e-01 -1.519e-01 -2.800e-01 -3.681e-03 +5.489e-02 -2.441e-01 -7.445e-02 +2.477e-01 +1.023e-01 +1.551e-01 +3.243e-01 -1.507e-02 )
     ))

#;(wfs-set-g-vector!
   #(0.10 0.10 0.05 0.05 0.05 0.05 0.05))

(wfs-set-window-center! 732 360)
(wfs-configure! 'full-frame-1)

(wfs-set-r-vector!
 (vector-shift +0 +0
               #(
                 723.669446562545 396.479057465993
                 724.051190655488 413.291519543038
                 740.40198539014 396.094882468168
                 740.71498938163 412.859644454184
                 )))

(wfs-set-i-matrix!
 #(
   #(-6.56583380290335 0.474936618450176 -6.71650627334095 0.071485537459471 -6.55878679361367 0.334449256990431 -6.69036165980017 0.239535212430361)
   #(-0.0875897059840961 5.61163249371679 0.315371351306632 5.79109131388011 0.173216783461735 6.14308073758624 0.119565708437166 6.02684995818772)
   #(-0.337347344674765 -0.565903944717723 -0.357267638422874 0.498946521893085 0.667885855512392 -0.557403003940906 0.647650382732705 0.498710419570104)
   #(-0.354208797110582 -0.4754440549889 -0.15381712954064 -0.643386730313811 -0.254340979037389 -0.239643839765833 -0.0884154549192849 -0.368884076803868)
   #(-0.448270475118107 0.433272083955332 -0.573597738810589 0.104512603340739 -0.218962019859412 0.318996852088335 -0.373552536304342 0.0491944095190316)
   ))

(wfs-set-i-matrix!
 #(
   #(-7.34827651173464 0.366302146481018 -7.46204509082669 0.308235745697117 -7.22462380973728 0.379564431672349 -7.3496217842027 0.207706145635819)
   #(0.903972827285445 6.40771115651413 1.01154542496548 6.86874827187569 1.04302450321869 6.82891027674458 1.10127739081278 6.96381542653647)
   #(-0.433906023796297 -0.490605768714004 -0.394709742820876 0.318790499323085 0.400851603912871 -0.606685554331451 0.476741386446567 0.318150283400463)
   #(-0.254666085118354 -0.0490669264116188 -0.0548163233264947 -0.182245425907311 -0.129244753037392 0.141803399185427 0.055646123033593 0.0224542020069269)
   #(-0.0949118001760575 0.22360512967724 -0.205315752116455 0.0491990247737206 0.0941558289519683 0.153665067211083 -0.0201316273592648 -0.0483038761235339)
   ))

(wfs-set-c-matrix!
 #(
   #(  -3.3876e-02   5.1186e-03  -3.4351e-02   5.0922e-03  -3.3224e-02   5.4045e-03  -3.3777e-02   4.6727e-03)
   #(   9.6969e-04   3.4755e-02   1.4894e-03   3.7211e-02   1.7854e-03   3.7034e-02   2.0331e-03   3.7670e-02)
   #(  -3.0530e-01  -2.6567e-01  -3.0947e-01   2.2363e-01   2.8803e-01  -3.6155e-01   3.0716e-01   1.9647e-01)
   #(  -1.7002e+00  -6.0866e-01  -1.4375e-01  -1.3627e+00  -1.0386e+00   8.3771e-01   4.1523e-01   2.1354e-01)
   #(  -5.1817e-01   1.6114e+00  -1.5720e+00   6.6350e-01   9.5816e-01   8.3278e-01  -9.7946e-02  -3.0930e-01)))


;; For 3x3 cross.

(wfs-set-r-vector!
 (vector-shift +2 -40
               #(
                 718.508028345892 405.972909939003
                 734.818103876179 388.818735616624
                 735.210407759849 405.470371902726
                 735.512990936556 422.25084824541
                 752.023007320511 405.11226299277)))

(wfs-set-i-matrix!
 #(
   #(-7.52568393836497 0.347673234539948 -7.23372983952229 0.333349842207539 -7.34245614001358 0.142700017725682 -7.31470172050307 0.297248300347699 -7.50143885658657 0.399688552860533)
   #(1.10776731391264 6.80231727257446 1.12027166009087 6.99284314056453 1.09509517651305 6.78761181254158 1.07045691862943 6.72744109093008 0.931463621442416 6.93108710391488)
   #(-0.635080373362739 0.167393214369489 0.146801427568543 -0.678837267913923 0.219355853611248 0.130301561124259 0.227940666966412 0.889850650536983 0.992446594646191 0.152665606219619)
   #(-0.0968034624131639 -0.222928165067813 -0.163760628094889 0.0881112694034668 0.00999394371323466 -0.00877139848025266 0.18300866159492 -0.100058526520524 0.131420140124617 0.179927230536587)
   #(-0.180418785680362 0.115605147504292 0.104623437885774 0.19274267403985 -0.00899171324673512 0.000218556090371654 -0.12520254082674 -0.175049426323299 0.161298920426066 -0.10306061444763)))

(wfs-set-c-matrix!
 #(
   #( -2.741e-02 +4.320e-03 -2.632e-02 +4.351e-03 -2.674e-02 +3.553e-03 -2.665e-02 +4.099e-03 -2.740e-02 +4.570e-03 )
   #( +1.318e-03 +2.883e-02 +1.500e-03 +2.963e-02 +1.346e-03 +2.868e-02 +1.254e-03 +2.850e-02 +5.851e-04 +2.940e-02 )
   #( -2.237e-01 +8.567e-02 +7.221e-02 -2.418e-01 +7.662e-02 +4.716e-02 +5.826e-02 +3.187e-01 +3.466e-01 +3.272e-02 )
   #( -5.209e-01 -1.181e+00 -8.630e-01 +6.046e-01 +2.983e-02 -5.979e-02 +9.239e-01 -6.834e-01 +6.670e-01 +9.219e-01 )
   #( -1.082e+00 +5.890e-01 +5.455e-01 +1.095e+00 -3.873e-02 +3.668e-03 -6.405e-01 -9.901e-01 +9.995e-01 -5.190e-01 )))

(wfs-set-c-matrix!
 #(
   #( 0 0 0 0 -2.674e-02 +3.553e-03 0 0 0 0 )
   #( 0 0 0 0 +1.346e-03 +2.868e-02 0 0 0 0 )
   #( -2.237e-01 +8.567e-02 +7.221e-02 -2.418e-01 +7.662e-02 +4.716e-02 +5.826e-02 +3.187e-01 +3.466e-01 +3.272e-02 )
   #( -5.209e-01 -1.181e+00 -8.630e-01 +6.046e-01 +2.983e-02 -5.979e-02 +9.239e-01 -6.834e-01 +6.670e-01 +9.219e-01 )
   #( -1.082e+00 +5.890e-01 +5.455e-01 +1.095e+00 -3.873e-02 +3.668e-03 -6.405e-01 -9.901e-01 +9.995e-01 -5.190e-01 )))

(wfs-set-g-vector! #(0.4 0.4 0.1 0.0 0.0))
(wfs-set-search-radius! 6)

(define (wfs-tune! refinement-radius g-vector)
  (wfs-set-refinement-radius! refinement-radius)
  (wfs-set-g-vector g-vector))

(wfs-configure! 'sub-frame-2)

(define (list-rms a)
  (sqrt (/ (apply + (map (lambda (x) (* x x)) a))
           (length a))))

(wfs-set-scale! (* (instrument-scale) (list-rms (list (/ (list-rms (vector->list (vector-ref (instrument-i-matrix) 0)))
                                                         (list-rms (vector->list (vector-ref (wfs-i-matrix) 0))))
                                                      (/ (list-rms (vector->list (vector-ref (instrument-i-matrix) 1)))
                                                         (list-rms (vector->list (vector-ref (wfs-i-matrix) 1))))))))

(log-info "")
(log-info "finished configuring wfs.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring wfc.")
(log-info "")

(wfc-set-a-flat-vector! #(-0.182402920110107 0.0696921441477224 -0.190937563826291 0.0 0.0))

(wfc-flatten!)

(log-info "finished configuring wfc.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
